package net.xumf.support.auth;

import net.xumf.domain.master.SystemUser;
import net.xumf.support.enums.CodeEnum;
import net.xumf.support.exception.SystemException;

import javax.security.auth.login.AccountException;

public class CurrentUser {

    private ThreadLocal<SystemUser> threadLocal = new ThreadLocal<>();

    private static CurrentUser currentUser;

    private CurrentUser() {
    }

    public static synchronized CurrentUser instance() {

        if (null == currentUser) {
            currentUser = new CurrentUser();
        }
        return currentUser;
    }

    public SystemUser get() {
        SystemUser systemUser = threadLocal.get();
        if (null == systemUser) {
            throw new SystemException(CodeEnum.UNAUTHORIZED);
        }
        return systemUser;
    }

    public void set(SystemUser user) {
        threadLocal.set(user);
    }
}
